<?php
// 本类由系统自动生成，仅供测试用途
class LsnAction extends Action{
	
	
//	DROP TABLE IF EXISTS `nce`.`nce_sentence`;
//CREATE TABLE  `nce`.`nce_sentence` (
//  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
//  `en_text` varchar(255) NOT NULL,
//  `cn_text` varchar(255) NOT NULL,
//  `stime` int(11) NOT NULL,
//  `etime` int(11) NOT NULL,
//  `term` int(11) NOT NULL,
//  `lesson` int(11) NOT NULL,
//  `words_explain` varchar(255) DEFAULT NULL,
//  `idx` int(11) NOT NULL,
//  `usstime` int(11) NOT NULL,
//  `usetime` int(11) NOT NULL,
//  `gs` varchar(255) DEFAULT NULL,
//  PRIMARY KEY (`id`)
//) ENGINE=MyISAM AUTO_INCREMENT=5016 DEFAULT CHARSET=utf8;
	public function merge($idPre,$idCrt) {
		$Sentence = M('Sentence');
		$stcCrt =  $Sentence->where("id=$idCrt")->select();
		$stcPre =  $Sentence->where("id=$idPre")->select();
		dump($stcCrt[0]["en_text"] );
		
		
		$data['en_text'] = $stcPre[0]["en_text"].$stcCrt[0]["en_text"];
		$data['cn_text'] = $stcPre[0]["cn_text"].$stcCrt[0]["cn_text"];
		$data['etime'] = $stcCrt[0]["etime"];
		$data['usetime'] = $stcCrt[0]["usetime"];
		$Sentence->where("id=$idPre")->save($data);
		
		$Sentence->delete("$idCrt");
	}
	
	public function decreaseIdx($id,$idx) {
		$Sentence = M('Sentence');
		$data['idx'] = $idx;
		$Sentence->where("id=$id")->save($data);
	}

	public function setSentenceUsEtime($id,$ms) {
		$Sentence = M('Sentence');
		$data['usetime'] = $ms;
		$Sentence->where("id=$id")->save($data);
	}

	public function setSentenceUsStime($id,$ms) {
		$Sentence = M('Sentence');
		$data['usstime'] = $ms;
		$Sentence->where("id=$id")->save($data);
	}

	public function setSentenceEtime($id,$ms) {
		$Sentence = M('Sentence');
		$data['etime'] = $ms;
		$Sentence->where("id=$id")->save($data);
	}

	public function setSentenceStime($id,$ms) {
		$Sentence = M('Sentence');
		$data['stime'] = $ms;
		$Sentence->where("id=$id")->save($data);
	}

	public function getLsn($lesson,$term){
		$Sentence = M('Sentence');
		$list = $Sentence->where("lesson=$lesson and term=$term")->order("idx")->select();
		return $list;
	}
	
	public function getLessons($term){
		$Lesson = M('Lesson');
		$list = $Lesson->where("term=$term")->order("lesson")->select();
		return $list;
	}
	
	public function initLsn6(){
		if ($fh = fopen("D:/NewConceptStudy307/Title/4.txt",'r')) {
			while (! feof($fh)) {
				$txt = $txt.fgets($fh);
			}
		}
		//print $txt;
		$stcArr = explode("^", $txt);
		//print count($stcArr);
		for($i = 1,$size = count($stcArr); $i < $size ; $i++){
			echo "<br>";
			echo $i*2-1;
			
			echo  $stcArr[$i];
			$LessonDB = M('Lesson');
			$lesson['lesson']=$i;
			$lesson['term']=4;
			$lesson['title']=iconv( "GBK", "UTF-8" ,$stcArr[$i]);
			$LessonDB->add($lesson);
			
		}
	}

	//导入词库的上万个单词
	public function importAllwords(){
		if ($fh = fopen("d:/words.txt",'r')) {
			while (! feof($fh)) {
				$txt = $txt.fgets($fh);
			}
		}
		//print $txt;
		$stcArr = explode("$", $txt);
		print count($stcArr);
		for($i = 0,$size = count($stcArr); $i < $size ; $i++){
			$wordStr = $stcArr[$i];
			$newwordDB = M('newword');
			//			$newword['lessson']=$k;
			//			$newword['term']=$crtTerm;


			//
			//$wordStr = "about[2'baut]ad.周围；大约；prep.关于；到处；忙于";
			$wordStrArr = explode( "[",$wordStr);
			$newword['en'] = $wordStrArr[0];
			$wordcnArr = explode("]",$wordStrArr[1]);
			$newword['soundmark']=$wordcnArr[0];
			$newword['cn']=iconv( "GBK", "UTF-8" , $wordcnArr[1]);
			$newwordDB->add($newword);

		}
			
	}

	//从背单词网站导入单词数据
	public function  initLsn2(){
		$term = 1;
		for($lesson = 3 ; $lesson < 61;$lesson++){
			$url = "http://dict.cn/bdc/218-".$lesson;
			if($lesson%2==0){
				$lesson = $lesson-0.5;
			}
			//echo $url;
			$contents = file_get_contents($url);
			$startPos = strpos($contents, "<tbody id=\"wordList\">");
			$endPos = strpos($contents,"</tbody>",$startPos);
			$contents = substr($contents, $startPos,$endPos-$startPos);
			//如果出现中文乱码使用下面代码
			$contents = iconv("GBK", "UTF-8",$contents);
			//echo $contents;
			$contentsArr = explode("</tr>", $contents);
			for($i = 0,$size = count($contentsArr); $i < $size ; $i++){
				$wordArr = explode("</td>",$contentsArr[$i] );
				$startPos = strpos($wordArr[1], "false;\">");
				$endPos = strpos($wordArr[1],"<",$startPos);
				//echo $endPos-$startPos;
				$worden = substr($wordArr[1], $startPos+8,$endPos-$startPos-8);
				$startPos = strpos($wordArr[2], "<td>");
				$endPos = strpos($wordArr[2],"||0",$startPos);
				$wordsoundmark = substr($wordArr[2], $startPos+4,$endPos-$startPos-4);
				$newwordDB = M('newword');
				$newword['lesson']=$lesson;
				$newword['term']=$term;
				//$newword['en']=$worden;
				$newword['realmark']=$wordsoundmark;
				$isupdate =  $newwordDB->where("term = $term and en='$worden'")->save($newword);
				if($isupdate==0){
					echo $worden."[".$wordsoundmark."]<br>";
				}
					
			}
		}
		//echo $contents;

	}

	//解析文本生成课文数据
	public  function initLsn3(){

		$crtTerm =3;

		for($k = 34; $k <35;$k++){

			$txt = "";
			//$l = ($k*2)-1;
			if ($fh = fopen("C:/Users/guangtou/Documents/$crtTerm/$k.txt",'r')) {
				while (! feof($fh)) {
					$txt = $txt.fgets($fh);
				}
			}
			$txt = str_replace("|", "", $txt);
			$stcArr = explode("*", $txt);
			dump($stcArr);
			//		print $txt;

			for($i = 0,$size = count($stcArr); $i < $size ; $i++){
				//处理每一句的素材
				if(strlen(trim($stcArr[$i])) > 0){
					$stc = trim($stcArr[$i]);

					//分中英文--单词和时间标记
					$offset =  strpos($stc, "#000#");
					$encnStr = substr($stc, 0,$offset);
//					print $encnStr;

					$wordsSoundoffset = substr($stc, $offset+5);
					//echo  $wordsSoundoffset;


					//解析中英文课文
					$offset = strpos($encnStr,"#");
					$enStr = substr($encnStr,0,$offset);
					$cnStr = substr($encnStr, $offset+1);
					//print $enStr;

					$sentence['en_text'] = $enStr;
					$sentence['cn_text'] = iconv( "GBK", "UTF-8" , $cnStr);


					//解析单词
					$offset = strpos($wordsSoundoffset, "#");
					$wordStr = substr($wordsSoundoffset, 0,$offset);
					$wordStr = trim($wordStr);
					if(strlen($wordStr) > 0){
						$words = explode( "\n",$wordStr);
					}
					$sentence['words_explain'] = iconv( "GBK", "UTF-8" , $wordStr);
					//		lsn.setWords(words);

					//解析时间标记
					$soundStr = substr($wordsSoundoffset, $offset+1,strlen($wordsSoundoffset)-5-$offset);
					$soundTags = explode("#", $soundStr);
					$sentence['stime']=$soundTags[0]*1000+16000;
					$sentence['etime']=$soundTags[1]*1000+16000;
					$sentence['usstime']=$soundTags[0]*1000+13000;
					$sentence['usetime']=$soundTags[1]*1000+13000;
					$sentence['lesson']=$k;
					$sentence['term']=$crtTerm;
					$sentence['idx']=$i;
					$sentenceDB = M('sentence');
					$sentenceDB->add($sentence);
					//$sentenceDB->where("lesson=$k and term = $crtTerm and idx=$i" )->save($sentence);
//					$enWords = explode(" ",$enStr." ");
//					//dump($enWords);
//					for($j=0,$size1=count($enWords);$j<$size1;$j++){
//						$firstPos = strpos($enWords[$j], "^");
//						if($firstPos > 0){
//							$tttstr = $worden;
//							$worden =  substr($enWords[$j],0,$firstPos);
//							$newwordDB = M('newword');
//							$newword['lesson']=$k;
//							$newword['term']=$crtTerm;
//							//$newword['en']=$worden;
//							$newword['lesson_idx']=$i;
//							$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//							if($isupdate == 0){
//
//								$tmpStr = substr($worden,strlen($worden)-1);
//								//	echo "尾巴是----".$tmStr;
//								if($tmpStr=="g"){
//									$worden = substr($worden, 0,strlen($worden)-3);
//									//echo "--修正后为:".$worden;
//									$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//									if($isupdate==0){
//										$worden = $worden."e";
//										$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//										if($isupdate==0){
//											echo "\n".$k."--$i--".$enWords[$j];
//										}else{
//											//echo "修正成功";
//										}
//									}else{
//										//echo "修正成功";
//									}
//								}else if($tmpStr=="d"){
//									$worden = substr($worden,0, strlen($worden)-1);
//									//echo "--修正后为:".$worden;
//									$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//									if($isupdate==0){
//										$worden = substr($worden,0, strlen($worden)-1);
//										$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//										if($isupdate==0){
//											$worden = substr($worden,0, strlen($worden)-1);
//											$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//											if($isupdate==0){
//												echo "\n".$k."--$i--".$enWords[$j];
//											}else{
//												//echo "修正成功";
//											}
//										}else{
//											//echo "修正成功";
//										}
//									}else{
//										//echo "修正成功";
//									}
//								}else if($tmpStr=="s"){
//									$worden = substr($worden,0, strlen($worden)-1);
//									//echo "--修正后为:".$worden;
//									$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//									if($isupdate==0){
//										$worden = substr($worden,0, strlen($worden)-1);
//										$isupdate =  $newwordDB->where("en='$worden'")->save($newword);
//										if($isupdate==0){
//											echo "\n".$k."--$i--".$enWords[$j];
//										}else{
//											//echo "修正成功";
//										}
//									}else{
//										//echo "修正成功";
//									}
//								}
//									
//							}
//						}
//					}
//--------------------------------------------------------------------------------------
					

					//					$Sentence = M('Sentence');
					//					$data['usstime'] = $soundTags[0]*1000-3000;
					//					$data['usetime'] = $soundTags[1]*1000-3000;
					//
					//					$Sentence->where("lesson=$k and term = $crtTerm and idx=$i" )->save($data);


				}
			}
		}
	}
}
?>